import 'package:flutter/material.dart';

class PageIndicator extends StatelessWidget {
  const PageIndicator({
    Key? key,
    this.itemCount = 1,
    this.selectItem = 0,
    this.normalColor = const Color(0x5FFFFFFF),
    this.selectedColor = Colors.white,
    this.size = 5.0,
    this.spacing = 6.0,
  }) : super(key: key);

  /// 指示器的个数
  final int itemCount;

  final int selectItem;

  /// 普通的颜色
  final Color normalColor;

  /// 选中的颜色
  final Color selectedColor;

  /// 点的大小
  final double size;

  /// 点的间距
  final double spacing;

  /// 点的Widget
  Widget _buildIndicator(
      int index, int pageCount, double dotSize, double spacing) {
    // 是否是当前页面被选中
    bool isCurrentPageSelected = index == selectItem;

    return SizedBox(
      height: size,
      width: size + spacing,
      child: Center(
        child: Material(
          color: isCurrentPageSelected ? selectedColor : normalColor,
          type: MaterialType.circle,
          child: SizedBox(
            width: dotSize,
            height: dotSize,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: List<Widget>.generate(itemCount, (int index) {
        return _buildIndicator(index, itemCount, size, spacing);
      }),
    );
  }
}
